<?php
// @formatter:off
/**
 * @file WCTheme.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-22 17:58:11 +0000 (Sat, 22 Jun 2013) $
 *
 * $Id: WCTheme.php 83 2013-06-22 17:58:11Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/includes/WCTheme.php $
 */
// @formatter:on

/**
 * @class WCTheme
 * @abstract
 */
class WCTheme {
	protected $_name;
	protected $_parser = false;
	protected $_themeRoot = false;
	protected $_themeUri = false;
	//
	// Construction.
	public function __construct() {
		$this->load();
		$this->loadRightTools();
		$this->loadRightAdds();
	}
	//
	// Public methods.
	public function loadAction($action) {
		die(__CLASS__."::".__FUNCTION__."() Subclass Resposabillity\n");
	}
	public function name() {
		return $this->_name;
	}
	public function & parser() {
		return $this->_parser;
	}
	public function root() {
		return $this->_themeRoot;
	}
	public function uri() {
		return $this->_themeUri;
	}
	//
	// Protected methods.
	protected function load() {
		die(__CLASS__."::".__FUNCTION__."() Subclass Resposabillity\n");
	}
	/**
	 * This method load all right advertisments managers and also set their
	 * templates to be shown
	 */
	protected function loadRightAdds() {
		//
		// Loading global pointers.
		// @{
		global $wcRightAdds;
		global $wcThemeAssigns;
		global $wcDefaults;
		// @}
		//
		// Creating an auxiliar list for accepted templates.
		$list = array();
		//
		// Going through all right tools and analyzing them.
		foreach($wcRightAdds as $add) {
			//
			// Checking if the right tool requires some PHP analysis.
			if(isset($add["manager"])) {
				//
				// Checking if the PHP file required exists. If it
				// does, it is included, otherwise an exception is
				// raised.
				if(is_readable($add["manager"])) {
					require_once $add["manager"];
				} else {
					trigger_error(__CLASS__."::".__FUNCTION__."() Error: Unable to load '{$add["manager"]}'", E_USER_ERRORu);
				}
			}
			//
			// If no valid template was specified, an exception is
			// raised.
			if(!is_readable($add["template"])) {
				trigger_error(__CLASS__."::".__FUNCTION__."() Error: Unable to load '{$add["template"]}'", E_USER_ERRORu);
			}
			//
			// Checking how many times this ad should be included.
			$times = isset($add["times"]) ? $add["times"] : 1;
			//
			// Including this ad as many times as needed.
			$time = 0;
			while($time < $times) {
				//
				// Setting a global indicator of iteration.
				$wcDefaults["WCTheme::time"] = $time;
				//
				// Auxiliar flag.
				$ok = true;
				//
				// If there is a function specified, it is executed and if
				// it returns true, the tool is added.
				if(isset($add["checker"])) {
					$ok = $add["checker"]();
				}
				//
				// If everything is ok, the tool is added to the list.
				if($ok) {
					$list[] = array("template" => $add["template"]);
				}
				//
				// Increasing iteration counter.
				$time++;
			}
		}
		//
		// Setting the list to be shown.
		if(count($list)) {
			$wcThemeAssigns["WC-RIGHT-ADDS"] = $list;
		}
	}
	/**
	 * This method load all right tools managers and also set their templates
	 * to be shown
	 */
	protected function loadRightTools() {
		//
		// Loading global pointers.
		// @{
		global $wcRightTools;
		global $wcThemeAssigns;
		global $wcDefaults;
		// @}
		//
		// Creating an auxiliar list for accepted templates.
		$list = array();
		//
		// Going through all right tools and analyzing them.
		foreach($wcRightTools as $tool) {
			//
			// Checking if the right tool requires some PHP analysis.
			if(isset($tool["manager"])) {
				//
				// Checking if the PHP file required exists. If it
				// does, it is included, otherwise an exception is
				// raised.
				if(is_readable($tool["manager"])) {
					require_once $tool["manager"];
				} else {
					trigger_error(__CLASS__."::".__FUNCTION__."() Error: Unable to load '{$tool["manager"]}'", E_USER_ERRORu);
				}
			}
			//
			// If no valid template was specified, an exception is
			// raised.
			if(!is_readable($tool["template"])) {
				trigger_error(__CLASS__."::".__FUNCTION__."() Error: Unable to load '{$tool["template"]}'", E_USER_ERRORu);
			}
			//
			// Checking how many times this tool should be included.
			$times = isset($tool["times"]) ? $tool["times"] : 1;
			//
			// Including this tool as many times as needed.
			$time = 0;
			while($time < $times) {
				//
				// Setting a global indicator of iteration.
				$wcDefaults["WCTheme::time"] = $time;
				//
				// Auxiliar flag.
				$ok = true;
				//
				// If there is a function specified, it is executed and if
				// it returns true, the tool is added.
				if(isset($tool["checker"])) {
					$ok = $tool["checker"]();
				}
				//
				// If everything is ok, the tool is added to the list.
				if($ok) {
					$aux = array("template" => $tool["template"]);
					if(isset($tool["title"])) {
						$aux["title"] = $tool["title"];
					}

					$list[] = $aux;
				}
				//
				// Increasing iteration counter.
				$time++;
			}
		}
		//
		// Setting the list to be shown.
		$wcThemeAssigns["WC-RIGHT-TOOLS"] = $list;
	}
	//
	// Public class methods.
	public static function Install() {
		return true;
	}
}
?>